<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目名称" prop="projectManageName">
        <el-input
          v-model="queryParams.projectManageName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目负责人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入项目负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:detail:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="项目名称" align="center" prop="projectManageName"/>
      <el-table-column label="项目负责人" align="center" prop="createName"/>
      <el-table-column label="项目接收人" align="center" prop="userName"/>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-takeaway-box"
            @click="handleDraft(scope.row)"
            v-hasPermi="['system:detail:draft']"
            :disabled="scope.row.state==='3'"
          >草稿
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleSubmit(scope.row)"
            v-hasPermi="['system:detail:submit']"
            :disabled="scope.row.state==='3'"
          >提交
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-chat-dot-square"
            @click="handleRemark(scope.row)"
            v-hasPermi="['system:remark:query']"
          >评论
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:detail:edit']"
            :disabled="scope.row.state==='3'"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:detail:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目管理详情对话框 -->
    <el-dialog :title="title" :visible.sync="remarkVisible" width="60%" append-to-body>
      <el-row>
        <el-button style="margin-left: 95%" icon="el-icon-refresh" @click="getRemarkList()" circle></el-button>
      </el-row>
      <div>
        <div v-clickoutside="hideReplyBtn" @click="inputFocus" class="my-reply">
          <el-avatar class="header-img" :size="40" :src="myHeader"></el-avatar>
          <div class="reply-info">
            <div
              tabindex="0"
              contenteditable="true"
              id="replyInput"
              spellcheck="false"
              placeholder="输入评论..."
              class="reply-input"
              @focus="showReplyBtn"
              @input="onDivInput($event)"
            >
            </div>
          </div>
          <div class="reply-btn-box" v-show="btnShow">
            <el-button class="reply-btn" size="medium" @click="sendComment" type="primary">发表评论</el-button>
          </div>
        </div>
        <div v-for="(item,i) in comments" :key="i" class="author-title reply-father">
          <el-avatar class="header-img" :size="40" :src="myHeader"></el-avatar>
          <div class="author-info">
            <span class="author-name">{{ item.name }}</span>
            <span class="author-time">{{ item.time }}</span>
          </div>
          <div class="icon-btn">
                <span @click="showReplyInput(i,item.name,item.id)">
                    <i class="iconfont el-icon-s-comment"></i>{{ item.commentNum }}</span>
            <!--        <i class="iconfont el-icon-caret-top"></i>{{item.like}}-->
          </div>
          <div class="talk-box">
            <p>
              <span class="reply">{{ item.comment }}</span>
            </p>
          </div>
          <div class="reply-box">
            <div v-for="(reply,j) in item.reply" :key="j" class="author-title">
              <el-avatar class="header-img" :size="40" :src="myHeader"></el-avatar>
              <div class="author-info">
                <span class="author-name">{{ reply.from }}</span>
                <span class="author-time">{{ reply.time }}</span>
              </div>
              <div class="icon-btn">
                <span @click="showReplyInput(i,reply.from,reply.id)"><i
                  class="iconfont el-icon-s-comment"></i>{{ reply.commentNum }}</span>
                <!--            <i class="iconfont el-icon-caret-top"></i>{{reply.like}}-->
              </div>
              <div class="talk-box">
                <p>
                  <span>回复 {{ reply.to }}:</span>
                  <span class="reply">{{ reply.comment }}</span>
                </p>
              </div>
              <div class="reply-box">

              </div>
            </div>
          </div>
          <div v-show="_inputShow(i)" class="my-reply my-comment-reply">
            <el-avatar class="header-img" :size="40" :src="myHeader"></el-avatar>
            <div class="reply-info">
              <div tabindex="0" contenteditable="true" spellcheck="false" placeholder="输入评论..."
                   @input="onDivInput($event)" class="reply-input reply-comment-input"></div>
            </div>
            <div class=" reply-btn-box">
              <el-button class="reply-btn" size="medium" @click="sendCommentReply(i,j)" type="primary">发表评论
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>

    <!-- 评论 -->
    <el-dialog :title="titleAddRemark" :visible.sync="openRemark" width="500px" append-to-body>
      <el-form ref="formRemark" :model="formRemark" :rules="rulesRemark" label-width="80px">
        <el-form-item label="评论内容" prop="content">
          <el-input v-model="formRemark.content" type="textarea" placeholder="请输入内容" maxlength="500"
                    show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRemarkForm">确 定</el-button>
        <el-button @click="cancelRemark">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改项目管理详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="项目名称" prop="projectManageName">
          <el-input v-model="form.projectManageName" placeholder="请输入项目名称" style="width: 360px" :disabled="true"/>
        </el-form-item>
        <el-form-item label="项目负责人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入项目负责人" style="width: 360px" :disabled="true"/>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态" style="width: 360px">
            <el-option
              v-for="dict in dict.type.project_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="项目明细">
          <editor v-model="form.details" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addDetail, delDetail, draft, getDetail, listDetail, submit, updateDetail} from "@/api/system/detail";
import {listRemark, addRemark, delRemark} from "@/api/system/remark";

export default {
  name: "Detail",
  dicts: ['project_state'],
  data() {
    return {
      // 评论信息
      remarkList: [],
      titleAddRemark: "",
      flowerInfoId: null,
      openRemark: false,
      remarkVisible: false,
      loadingRemark: true,
      remarkTotal: 0,
      showSearchRemark: true,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目管理详情表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectManageName: null,
        userName: null,
        state: null,
      },
      // 查询参数
      remarkQueryParams: {
        pageNum: 1,
        pageSize: 20,
        name: null,
        projectManageDetailId: null
      },
      // 表单参数
      form: {},

      formRemark: {},
      // 表单校验
      rules: {
        projectManageName: [
          {required: true, message: "项目名称不能为空", trigger: "blur"}
        ],
        createName: [
          {required: true, message: "项目负责人不能为空", trigger: "blur"}
        ],
        state: [
          {required: true, message: "状态不能为空", trigger: "blur"}
        ],
      },
      // 表单校验
      rulesRemark: {
        content: [
          {required: true, message: "评论内容不能为空", trigger: "blur"}
        ]
      },

      btnShow: false,
      index: '0',
      replyComment: '',
      myName: 'Lana Del Rey',
      myHeader: require("@/assets/images/profile.jpg"),
      myId: 1,
      to: '',
      toId: -1,

      //评论数据集合
      comments: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 表单重置
    resetRemark() {
      this.formRemark = {
        id: null,
        pid: null,
        flowerInfoId: null,
        userId: null,
        userName: null,
        content: null,
        delFlag: null,
        createName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("formRemark");
    },
    cancelRemark() {
      this.openRemark = false;
      this.resetRemark();
    },
    /** 提交按钮 */
    submitRemarkForm() {
      this.$refs["formRemark"].validate(valid => {
        if (valid) {
          this.formRemark.projectManageDetailId = this.projectManageDetailId
          addRemark(this.formRemark).then(response => {
            this.$modal.msgSuccess("评论成功");
            this.openRemark = false;
            this.formRemark.content = ""
            this.getRemarkList();
          });
        }
      });
    },
    /** 新增按钮操作 */
    handleAddRemark() {
      this.reset();
      this.openRemark = true;
      this.titleAddRemark = "发表评论";
    },
    /** 查询评论列表 */
    getRemarkList() {
      this.loadingRemark = true;
      this.resetRemark();
      listRemark(this.remarkQueryParams).then(response => {
        this.comments = response.data;
        this.loadingRemark = false;
      });
    },
    handleRemark(row) {
      this.remarkVisible = true;
      this.remarkQueryParams.projectManageDetailId = row.id;
      this.projectManageDetailId = row.id
      listRemark(this.remarkQueryParams).then(response => {
        this.comments = response.data;
        this.loadingRemark = false;
      });
    },

    /** 查询项目管理详情列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = Number(Number(response.total));
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectManageId: null,
        projectManageName: null,
        userId: null,
        userName: null,
        state: null,
        details: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目管理详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目管理详情";
      });
    },
    /** 草稿按钮操作 */
    handleDraft(row) {
      const id = row.id
      draft(id).then(response => {
        this.$modal.msgSuccess("保存草稿成功");
        this.getList();
      });
    },
    /** 提交按钮操作 */
    handleSubmit(row) {
      const id = row.id
      submit(id).then(response => {
        this.$modal.msgSuccess("提交项目成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目管理详情数据？').then(function () {
        return delDetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 删除评论按钮操作 */
    handleRemarkDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除评论？').then(function () {
        return delRemark(ids);
      }).then(() => {
        this.getRemarkList();
        this.$modal.msgSuccess("删除评论成功");
      }).catch(() => {
      });
    },

    inputFocus() {
      var replyInput = document.getElementById('replyInput');
      replyInput.style.padding = "8px 8px"
      replyInput.style.border = "2px solid blue"
      replyInput.focus()
    },
    showReplyBtn() {
      this.btnShow = true
    },
    hideReplyBtn() {
      let replyInput = document.getElementById('replyInput')
      this.btnShow = false
      replyInput.style.padding = "10px"
      replyInput.style.border = "none"
    },
    showReplyInput(i, name, id) {
      this.comments[this.index].inputShow = false
      this.index = i
      this.comments[i].inputShow = true
      this.to = name
      this.toId = id
    },
    _inputShow(i) {
      return this.comments[i].inputShow
    },
    sendComment() {
      if (!this.replyComment) {
        this.$message({
          showClose: true,
          type: 'warning',
          message: '评论不能为空'
        })
      } else {

        this.formRemark.projectManageDetailId = this.projectManageDetailId
        this.formRemark.content = this.replyComment
        addRemark(this.formRemark).then(response => {
          this.$modal.msgSuccess("评论成功");
          let input = document.getElementById('replyInput')
          this.replyComment = ''
          input.innerHTML = '';
          this.openRemark = false;
          this.getRemarkList();
        });
      }
    },
    sendCommentReply(i, j) {
      if (!this.replyComment) {
        this.$message({
          showClose: true,
          type: 'warning',
          message: '评论不能为空'
        })
      } else {
        const tempData = this.comments[i];
        console.log("元数据", tempData)
        this.formRemark.projectManageDetailId = this.projectManageDetailId
        this.formRemark.content = this.replyComment
        this.formRemark.pid = tempData.id
        console.log(this.formRemark)
        addRemark(this.formRemark).then(response => {
          this.$modal.msgSuccess("评论成功");
          this.replyComment = ''
          document.getElementsByClassName("reply-comment-input")[i].innerHTML = ""
          this.openRemark = false;
          this.getRemarkList();
        });
      }
    },
    onDivInput: function (e) {
      this.replyComment = e.target.innerHTML;
    }
  }
};
</script>

<style scoped lang="css">
.my-reply {
  padding: 10px;
  background-color: #fafbfc;
}

.my-reply .header-img {
  display: inline-block;
  vertical-align: top;
}

.my-reply .reply-info {
  display: inline-block;
  margin-left: 5px;
  width: 90%;
}

@media screen and (max-width: 1200px) {
  .my-reply .reply-info {
    width: 80%;
  }
}

.my-reply .reply-info .reply-input {
  min-height: 20px;
  line-height: 22px;
  padding: 10px 10px;
  color: #ccc;
  background-color: #fff;
  border-radius: 5px;
}

.my-reply .reply-info .reply-input:empty:before {
  content: attr(placeholder);
}

.my-reply .reply-info .reply-input:focus:before {
  content: none;
}

.my-reply .reply-info .reply-input:focus {
  padding: 8px 8px;
  border: 2px solid #00f;
  box-shadow: none;
  outline: none;
}

.my-reply .reply-btn-box {
  height: 25px;
  margin: 10px 0;
}

.my-reply .reply-btn-box .reply-btn {
  position: relative;
  float: right;
  margin-right: 15px;
}

.my-comment-reply {
  margin-left: 50px;
}

.my-comment-reply .reply-input {
  width: flex;
}

.author-title:not(:last-child) {
  border-bottom: 1px solid rgba(178, 186, 194, 0.3);
}

.author-title {
  margin-top: 1rem;
  padding: 10px;
}

.author-title .header-img {
  display: inline-block;
  vertical-align: top;
}

.author-title .author-info {
  display: inline-block;
  margin-left: 5px;
  width: 60%;
  height: 40px;
  line-height: 20px;
}

.author-title .author-info > span {
  display: block;
  cursor: pointer;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.author-title .author-info .author-name {
  color: #000;
  font-size: 18px;
  font-weight: bold;
}

.author-title .author-info .author-time {
  font-size: 14px;
}

.author-title .icon-btn {
  width: 30%;
  padding: 0 !important;
  float: right;
}

@media screen and (max-width: 1200px) {
  .author-title .icon-btn {
    width: 20%;
    padding: 7px;
  }
}

.author-title .icon-btn > span {
  cursor: pointer;
}

.author-title .icon-btn .iconfont {
  margin: 0 5px;
}

.author-title .talk-box {
  margin: 0 50px;
}

.author-title .talk-box > p {
  margin: 0;
}

.author-title .talk-box .reply {
  font-size: 16px;
  color: #000;
}

.author-title .reply-box {
  margin: 10px 0 0 50px;
  background-color: #efefef;
}

</style>





